# MySQL Server API

## Server

- `createServer()` - creates server instance
- `Server.listen` - listen port / unix socket (same arguments as [net.Server.listen](https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback))

### Events

- **connect**
  - new incoming connection.

<hr />

## Connection

- `serverHandshake({ serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags })`
  - send server handshake initialisation packet, wait handshake response and start listening for commands
- `writeOk({ affectedRows: num, insertId: num })`
  - send [OK packet](https://dev.mysql.com/doc/internals/en/overview.html#packet-OK_Packet) to client
- `writeEof(warnings, statusFlags)`
  - send EOF packet
- `writeTextResult(rows, fields)`
  - write query result to client. Rows and fields are in the same format as in `connection.query` callback.
- `writeColumns(fields)`
  - write fields + EOF packets.
- `writeTextRow(row)`
  - write array (not hash!) of values as result row
- **TODO:** binary protocol

### Events

Every command packet received by the server will be emitted as a **packet** event with the parameters:

- **packet:** Packet
  - The packet itself
- **knownCommand:** boolean
  - is this command known to the server
- **\*commandCode:** number
  - the parsed command code (first byte)

In addition special events are emitted for [commands](https://dev.mysql.com/doc/internals/en/text-protocol.html) received from the client. If no listener is present a fallback behavior will be invoked.

- `quit()`
  - Default: close the connection
- `init_db(schemaName: string)`
  - Default: return OK
- `query(sql: string)`
  - Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
- `field_list(table: string, fields: string)`
  - Default: return ER_WARN_DEPRECATED_SYNTAX
- `ping()` - Default: return OK
